package com.erp.framework.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

import com.erp.framework.util.logger.Logger;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageUtil {
	public void deleteImageByPath(String path) throws Exception{
    	try {
    		if(!CommonUtil.isEmpty(path)){
    	    	File image = new File(path);
    	    	if(image.exists()) {
    	    		image.delete();
    		    	System.out.println(" Image deleted in Path: " + path);
    	    	}
    		}
    	} catch(Exception e) {
    		Logger.logExceptions(this, e);
	    	throw new Exception(e);
    	}
    }

	public void createImage(String path, String name, int height, int width, String imgType,  byte[] data) throws IOException {
		Image src = null;
		ByteArrayInputStream fs =  new   ByteArrayInputStream(data);
		src = javax.imageio.ImageIO.read(fs); // construct the Image Object
		if(height == 0){
			height= src.getHeight(null); // get the height
		}
		if(width == 0){
			width = src.getWidth(null); // get the width
		}
		BufferedImage tag = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
		tag.getGraphics().drawImage(src,0,0,width,height,null); 
	    if(path.length()>0 
	    		&& !path.substring(path.length()-1).equals("/") 
	    		&& !path.substring(path.length()-1).equals("\\")) {
	    	path += "/";
	    }
	    File file = new File(path);
	    FileOutputStream out = new FileOutputStream(path + name);
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		encoder.encode(tag);
		out.close();
		System.out.println("file created:" + name);
	}

	public void createImage(String imagePath, String imageName, String imageType, byte[] fileData) throws IOException {
		createImage(imagePath, imageName, 0, 0, imageType, fileData);
	}
	
	public static byte[] scaleImage(byte[] content, int maxWidth, int maxHeight)
			throws IOException {
		Image src = null;
		ByteArrayInputStream fs = new ByteArrayInputStream(content);
		src = javax.imageio.ImageIO.read(fs); 
		if (maxWidth < 0 || maxHeight < 0) {
			return content;
		}
		int width = src.getWidth(null) > maxWidth ? maxWidth : src
				.getWidth(null);
		int height = src.getHeight(null) > maxHeight ? maxHeight : src
				.getHeight(null);
		
		// Scales the image
		Image imageIn2 = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
		// Converts the Image to a BufferedImage
		BufferedImage scaled = new BufferedImage(imageIn2.getWidth(null),
				imageIn2.getHeight(null), BufferedImage.TYPE_INT_RGB);
		scaled.createGraphics().drawImage(imageIn2, 0, 0, null);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baos);
		encoder.encode(scaled);
		return baos.toByteArray();
	}
    
    public static byte[] convertImage(int height, int width, String imgType,  byte[] data) throws IOException {
        
        Image src = null;
        ByteArrayInputStream fs =  new   ByteArrayInputStream(data);
        src = javax.imageio.ImageIO.read(fs); // construct the Image Object
        if(height == 0){
            height= src.getHeight(null); // get the height
        }
        if(width == 0){
            width = src.getWidth(null); // get the width
        }
        BufferedImage tag = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        tag.getGraphics().drawImage(src,0,0,width,height,null); 
        return getImage(tag);

        
    }
    public static byte[] getImage(BufferedImage rendImage) {
        try {
            Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
            ImageWriter writer = iter.hasNext() ? (ImageWriter) iter.next() : null;

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
            writer.setOutput(ios);

            writer.write(new IIOImage(rendImage, null, null));
            writer.dispose();
            return baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
